package com.gongJiao.controller;


import com.gongJiao.Vo.AdminVehicleVo;
import com.gongJiao.Vo.PageCondition;
import com.gongJiao.Vo.PageResult;
import com.gongJiao.Vo.VehicleVo;
import com.gongJiao.common.Result;
import com.gongJiao.entity.Notice;
import com.gongJiao.entity.Route;
import com.gongJiao.entity.Type;
import com.gongJiao.entity.Vehicle;
import com.gongJiao.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  公告前端控制器
 * </p>
 * @author ${author}
 * @since 2021-12-25
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    /**
     * 后台分页查询公告信息列表
     * @return
     */
    @GetMapping("getNoticePageList")
    @ResponseBody
    public PageResult<Notice> getVehiclePageList(PageCondition pageCondition){
        PageResult<Notice> pageResult=new PageResult<Notice>();
            //查询所以数据
            List<Notice> list=noticeService.getNoticeList(pageCondition);
            //查询总记录数
            Integer count=noticeService.getCount(pageCondition);
            return pageResult.success(list,count);
    }

    /**
     * 添加公告
     * @param notice
     * @return
     */
    @PostMapping("addNotice")
    @ResponseBody
    public Result addVehicle(Notice notice){
        notice.setCreateTime(new Date());
        boolean save = noticeService.save(notice);
        if (save) {
            return Result.success("新增成功");
        }
        return Result.fail("新增失败");
    }

    /**
     * 根据id获取公告信息
     * @param id
     * @return
     */
    @GetMapping("/getNotice/{id}")
    public String getNoticeById(@PathVariable Long id, Model model){
        //获取公告信息
        Notice notice=noticeService.getNoticeById(id);
        model.addAttribute("notice",notice);
        return "admin/noticeEdit";
    }


    /**
     * 修改信息
     * @param notice
     * @return
     */
    @PostMapping("updateNotice")
    @ResponseBody
    public Result updateNotice(Notice notice){
        notice.setCreateTime(new Date());
        Boolean flag=noticeService.updateNotice(notice);
        if (flag) {
            return Result.success("公告信息修改成功！");
        }
        return Result.fail("公告信息修改失败！");
    }

    /**
     * 删除公告信息
     * @param ids
     * @return
     */
    @PostMapping("/{ids}")
    @ResponseBody
    public Result deleteNoticeByIds(@PathVariable("ids") String ids){
        noticeService.deleteNoticeByIds(ids);
        return Result.success("删除员工成功！");
    }

}

